<template>
	<view class="box">
		<view class="title">草地调查监测系统</view>
		<view class="login_input">
			<uni-easyinput class="lastinput" prefixIcon="person-filled" v-model="params.username" placeholder="请输入账号">
			</uni-easyinput>
			<uni-easyinput type="password" prefixIcon="locked-filled" class="uni-mt-5" v-model="params.password"
				placeholder="请输入登录密码"></uni-easyinput>
		</view>
		<view class="login_input">
			<button type="primary" @click="login">登录</button>
		</view>

	</view>
</template>

<script>
	import {
		getNetWork
	} from '@/core/unicommon/deviceInfo.js'
	import {
		login,
		userinfo,
		getSubsurfaceBiomass,
		getGrasslandType,
		getEnvironment,
		getYfList,
		getBiomass,
		getYdList,
		projects,
		organizationTree

	} from '@/api/system.js'
	export default {
		components: {},
		// mixins: [sql],
		data() {
			return {

				params: {
					// username: "632823002",
					username: "630000-pA1",
					password: "",
					"clientId": "system",
					"clientSecret": "system",
					"grantType": "password",
					"scope": "app"
				},
				title: "正在登录请稍等...",
				userinfo: null,
				network: null,

			}
		},
		onLoad() {
			this.getLocalUserInfo()

		},
		mounted() {
			// #ifdef APP-PLUS 
			// this.openSQL()
			// this.creatUserTable()
			// setTimeout(this.insertUser, 300)
			// setTimeout(() => {
			// 	this.insertUser("李四")
			// }, 1000)
			// setTimeout(() => {
			// 	this.selectUser()
			// }, 2000)
			//  #endif
		},
		methods: {
			async getLocalUserInfo() {
				try {
					
					this.userinfo =await uni.getStorageSync("userinfo")
					this.userdata =await uni.getStorageSync("userdata")
					console.log(this.userinfo)
					console.log(this.userdata && this.userinfo && this.userinfo.id,this.userinfo)
					if (this.userdata && this.userinfo && this.userinfo.id) {
						uni.switchTab({
							url: "/pages/home/home"
						});
					}
					// this.params.username = this.userinfo.username
				} catch (e) {
					//TODO handle the exception
				}

				getNetWork().then(res => {
					this.network = res
					uni.setStorageSync("network", res.hasNetworkType)
				})

			},
			login() {


				if (!this.network) {
					if (this.userinfo) {
						uni.switchTab({
							url: "/pages/home/home"
						});
					} else {
						return uni.showToast({
							title: '请等待监测网络信息',
							icon: 'none'
						})
					}
				}

				let that = this
				console.log(111)
				if (this.params.username == '') {
					return uni.showToast({
						title: '账号不能为空',
						icon: 'none'
					})
				}
				if (this.params.password == '') {
					return uni.showToast({
						title: '密码不能为空',
						icon: 'none'
					})
				}
				if (this.network.hasNetworkType) {
					uni.showLoading({
						title: this.title,
						mask: true,
						icon: 'loading',
						success() {
							login(that.params).then(res => {
								if (res.resp_code != '10000') {
									return uni.showToast({
										title: '账号或密码错误',
										icon: 'none'
									})
								}
								uni.hideLoading()
								uni.setStorageSync('token', res.data.access_token)
								userinfo().then(it => {

									if (this.userinfo && this.userinfo.username != this.params
										.username) {
										try {
											uni.removeStorageSync('userdata');
										} catch (e) {

										}

									}

									uni.setStorageSync('userinfo', it.data)
									uni.navigateTo({
										url: '/pages/login/loadingData'
									})
									// uni.showLoading({
									// 	title: "登录成功，正在获取去用户信息",
									// 	mask: true,
									// 	icon: 'loading',
									// 	success() {
									// 		that.downLoadData(it.data.id).then(res => {
									// 			uni.hideLoading()
									// 			uni.setStorageSync('userinfo', it
									// 				.data)

									// 			uni.switchTab({
									// 				url: "/pages/home/home"
									// 			});
									// 		})

									// 	}

									// })
								}).catch(err => {



								}).finally(e => {
									console.log(e)
								})
							}).catch(err => {
								uni.hideLoading();
								console.log(err)
								if (that.userinfo && that.userinfo.username == that.params.username) {
									uni.showToast({
										title: "当前网络情况未知，采用本地数据登录",
										icon: 'none'
									})
									uni.switchTab({
										url: "/pages/home/home"
									});
								} else {
									uni.showToast({
										title: "登录失败",
										icon: 'none'
									})
								}
							})
						}
					})
				} else {
					if (this.userinfo) {
						uni.switchTab({
							url: "/pages/home/home"
						});
					} else {
						return uni.showToast({
							title: '当前没有缓存数据，请到网络覆盖范围内缓存数据',
							icon: 'none'
						})
					}

				}
			},
			showLoding(title, call) {
				uni.showLoading({
					title: title,
					mask: true,
					icon: 'loading',
					success() {
						call
					}
				})
			},
			async downLoadData(userid) {

				let surveyUser = userid
				let projectsRes = await projects({})
				//缓存样地数据
				let ydRes = await getYdList({
					surveyUser
				})
				let yd = ydRes.data.map(d => {
					d.action = "none"
					return d
				})
				//缓存样方数据
				let yfRes = await getYfList({
					surveyUser
				})
				let yf = yfRes.data.map(d => {
					d.action = 'none'
					return d
				})
				let biomassRes = await getBiomass({
					surveyUser
				})
				let biomass = biomassRes.data.map(d => {
					d.action = 'none'

					return d
				})


				let envRes = await getEnvironment({
					surveyUser
				})
				let env = envRes.data.map(d => {
					d.action = 'none'
					return d
				})
				let grassTypeRes = await getGrasslandType({
					surveyUser
				})
				let grassType = grassTypeRes.data.map(d => {
					d.action = 'none'
					return d
				})
				//底下生物量
				let subBiomassRes = await getSubsurfaceBiomass({
					surveyUser
				})
				let subBiomass = subBiomassRes.data.map(d => {
					d.action = 'none'
					return d
				})

				let orgTree = await organizationTree()
				console.log("orgTree", orgTree)
				//本地存储数据
				//storyage存储
				let userdata = {
					"projects": projectsRes.data.records,
					yd,
					yf,
					grassType,
					env,
					biomass,
					subBiomass,
					org: orgTree.data
				}
				uni.setStorageSync("userdata", userdata)
			},
			clearData() {

			},
			creatUserTable() {
				let sql = '"id" INTEGER PRIMARY KEY AUTOINCREMENT,"name" text,"age" number,"time" text'
				this.createTable("account", sql);
			},
			insertUser(user) {
				if (!user) {
					user = '张三'
				}
				let sql = ` '${user}',23,'2023-11-30'`
				console.log(sql)
				this.insertTableData("account", sql, 'name,age,time')
			},
			selectUser() {
				this.selectTableData("account")
			}

		}
	}
</script>

<style lang="less" scoped>
	::v-deep .uni-easyinput {
		width: 690rpx;
		height: 118rpx;
		background: #6E84A4;
		border-radius: 16rpx;
		margin-bottom: 88rpx;
	}

	::v-deep .is-input-border {
		height: 118rpx;
		border: none !important;
	}

	::v-deep .uni-input-input {
		color: #000;
		font-size: 30rpx;
	}

	::v-deep .uni-easyinput__placeholder-class {
		font-size: 30rpx;
		color: #ccc;
	}

	::v-deep uni-button {
		height: 118rpx;
		line-height: 118rpx;
	}

	.box {
		height: 100vh;
		padding-top: 40%;

		.title {
			text-align: center;
			font-size: 48rpx;

		}

		.login_input {
			width: 690rpx;
			margin: 0 auto;

			.lastinput {
				margin-top: 174rpx;
			}
		}
	}
</style>